var express = require('express');
const { time } = require('./libs/tools');
require('express-async-errors');
var router = express.Router();

var { pool, model, connection } = require(__dirname + '/libs/model');

function formatDate(number) {
    var now = new Date(number * 1000);
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var date = now.getDate();
    var hour = now.getHours();
    var minute = now.getMinutes();
    var second = now.getSeconds();
    return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
}
function unixDate(str) {
    return parseInt((new Date(str).valueOf()) / 1000);
}

function timezoneDate(str) {
    var now = new Date(str);
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var date = now.getDate();
    var hour = now.getHours();
    var minute = now.getMinutes();
    var second = now.getSeconds();
    return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
}

function nowDate() {
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var date = now.getDate();
    var hour = now.getHours();
    var minute = now.getMinutes();
    var second = now.getSeconds();
    return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
}

function nowUnix() {
    return parseInt((new Date().valueOf()) / 1000);
}

var timemachine = {
    //现在的时间戳
    ////https://www.cnblogs.com/fozero/p/6959946.html
    time: function () {
        return parseInt(new Date().getTime() / 1000);
    },
    //2019-12-12 23:59:59 转 时间戳
    strtotime: function (str) {
        if (!str) return 0;
        return parseInt(new Date(str).getTime() / 1000);
    },
    // 格式化日期，如月、日、时、分、秒保证为2位数
    formatNumber: function (n) {
        n = n.toString()
        return n[1] ? n : '0' + n;
    },
    // 参数number为毫秒时间戳，format为需要转换成的日期格式
    //data(value, 'Y-M-D h:m:s');
    date: function (number = '', format = 'Y-M-D h:m:s') {
        if (!number) {
            number = this.time();
        }
        //增加 2020-04-16T11:29:10.000Z 类型的处理
        let time = typeof (number) == 'object' ? number : new Date(number * 1000);
        let newArr = [];
        let formatArr = ['Y', 'M', 'D', 'h', 'm', 's'];
        newArr.push(time.getFullYear());
        newArr.push(this.formatNumber(time.getMonth() + 1));
        newArr.push(this.formatNumber(time.getDate()));

        newArr.push(this.formatNumber(time.getHours()));
        newArr.push(this.formatNumber(time.getMinutes()));
        newArr.push(this.formatNumber(time.getSeconds()));

        for (let i in newArr) {
            format = format.replace(formatArr[i], newArr[i])
        }
        return format;
    },
    datediff(str, secs, format = 'Y-M-D h:m:s') {
        let unix = this.strtotime(str) + secs;
        return this.date(unix, format);
    },
    //一天开始的时间戳
    dayStartEnd(str = '') {
        if (!str) str = this.date('', 'Y-M-D');
        let start = this.strtotime(str + ' 00:00:00');
        let end = this.strtotime(str + ' 23:59:59');
        return [start, end];
    },
    //https://blog.csdn.net/zxf1242652895/article/details/77853703
    //https://www.cnblogs.com/tlfe/p/12124204.html
    //一周开始的时间戳
    weekStartEnd() {
        let now = new Date(); // 当前日期
        let nowDayOfWeek = now.getDay(); // 今天本周的第几天
        let nowDay = now.getDate(); // 当前日
        let nowMonth = now.getMonth(); // 当前月
        let nowYear = now.getYear(); // 当前年
        nowYear += (nowYear < 2000) ? 1900 : 0;
        let day = nowDayOfWeek || 7;
        let start = parseInt(new Date(now.getFullYear(), nowMonth, nowDay + 1 - day).getTime() / 1000);
        let end = parseInt(new Date(now.getFullYear(), nowMonth, nowDay + 7 - day).getTime() / 1000) - 1;
        return [start, end];
    },
    //获取每月天数
    getMonthDays() {
        let now = new Date(); // 当前日期
        let nowMonth = now.getMonth(); // 当前月
        let nowYear = now.getYear(); // 当前年
        nowYear += (nowYear < 2000) ? 1900 : 0;
        let monthStartDate = new Date(nowYear, nowMonth, 1);
        let monthEndDate = new Date(nowYear, nowMonth + 1, 1);
        let days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
        return days;
    },
    //一月开始的时间戳
    monthStartEnd() {
        let now = new Date(); // 当前日期
        let nowMonth = now.getMonth(); // 当前月
        let nowYear = now.getYear(); // 当前年
        nowYear += (nowYear < 2000) ? 1900 : 0;
        let start = parseInt(new Date(nowYear, nowMonth, 1).getTime() / 1000);
        let end = parseInt(new Date(nowYear, nowMonth, this.getMonthDays()).getTime() / 1000) - 1;
        return [start, end];
    },
};

async function start() {
    console.log(timemachine.dayStartEnd());
    console.log(timemachine.weekStartEnd());
    console.log(timemachine.monthStartEnd());
    return;
    //await model.i("insert into time set ?", { addtime: '2020-04-16 10:19:40',edittime: '2020-04-16 19:29:10' });
    // dateStrings: false,
    /*
[
  RowDataPacket {
    itemid: 1,
    addtime: 2020-04-16T02:19:40.000Z,
    edittime: 2020-04-16T11:29:10.000Z
  }
]
    // dateStrings: true,
[
  RowDataPacket {
    itemid: 1,
    addtime: '2020-04-16 10:19:40',
    edittime: '2020-04-16 19:29:10'
  }
]
    */
    let d = await model.find("select * from time");
    //d['addtime'] = timezoneDate(d['addtime']);
    //d['edittime'] = timezoneDate(d['edittime']);

    d['addtime'] = timemachine.date(d['addtime']);
    d['edittime'] = timemachine.date(d['edittime']);
    console.log(d);

    console.log(timemachine.date());

    //console.log(nowDate());
    //console.log(nowUnix());

    connection.end(function (err) {
        console.log('connection closed');
        process.exit();
    });
}



start();